<?php

/**
 * This is the model class for table "attachments".
 *
 * The followings are the available columns in table 'attachments':
 * @property integer $id
 * @property integer $container_id
 * @property string $container_type
 * @property string $filename
 * @property string $local_filename
 * @property integer $filesize
 * @property string $content_type
 * @property string $digest
 * @property integer $downloads
 * @property integer $author_id
 * @property string $created_on
 * @property string $description
 *
 * The followings are the available model relations:
 * @property User $author
 */
class Attachment extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Attachment the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'attachments';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('container_id, filesize, downloads, author_id', 'numerical', 'integerOnly'=>true),
            array('container_type', 'length', 'max'=>30),
            array('filename, local_filename, content_type, description', 'length', 'max'=>255),
            array('digest', 'length', 'max'=>40),
            array('created_on', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('container_type, filename, local_filename, filesize, content_type, digest, downloads, created_on, description', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'author' => array(self::BELONGS_TO, 'User', 'author_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'container_id' => 'Container',
            'container_type' => 'Container Type',
            'filename' => 'Filename',
            'local_filename' => 'Disk Filename',
            'filesize' => 'Filesize',
            'content_type' => 'Content Type',
            'digest' => 'Digest',
            'downloads' => 'Downloads',
            'author_id' => 'Author',
            'created_on' => 'Created On',
            'description' => 'Description',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('container_type',$this->container_type,true);
        $criteria->compare('filename',$this->filename,true);
        $criteria->compare('local_filename',$this->local_filename,true);
        $criteria->compare('filesize',$this->filesize);
        $criteria->compare('content_type',$this->content_type,true);
        $criteria->compare('digest',$this->digest,true);
        $criteria->compare('downloads',$this->downloads);
        $criteria->compare('created_on',$this->created_on,true);
        $criteria->compare('description',$this->description,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
}